package com.zixun.controller;

import com.zixun.config.WebApiConfig;
import com.zixun.model.*;
import com.zixun.model.information.Information;
import com.zixun.model.information.InformationImage;
import com.zixun.repository.*;
import com.zixun.repository.information.InformationCommentRepository;
import com.zixun.repository.information.InformationImageRepository;
import com.zixun.repository.information.InformationRepository;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by Basil on 16/8/13.
 */
@RestController
@Transactional
@RequestMapping("/api/offer")
public class OfferWebApi {

    @Autowired
    OfferRepository offerRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    InformationCommentRepository informationCommentRepository;

    @Autowired
    InformationImageRepository informationImageRepository;

    @Autowired
    InformationRepository informationRepository;

    /**
     * @api {POST} /api/offer/userOffer 用户出价
     * @apiName userOffer
     * @apiGroup offer
     * @apiVersion 0.0.1
     * @apiDescription 用户出价
     * @apiParamExample 请求参数{JSON}
     * {
     * "userId":【用户编号 , 字符串 】,
     * "price":【价格，整型】,
     * "informationId":【资信编号，整型】,
     * }
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功,2:参数错误,3:服务器异常,10:用户不存在,11:JSON参数异常 整型】,
     * "errMsg":【错误信息 , 字符串】,
     * }
     */
    @RequestMapping("userOffer")
    public Object userOffer(@RequestBody String data) {
        Map<String, Object> result = new HashMap<>();
        try {
            JSONObject jo = new JSONObject(data);
            if(jo.isNull("userId")||jo.isNull("price")||jo.isNull("informationId")){
                result.put("errMsg", "参数错误");
                result.put("returnValue", 2);
                return result;
            }
            String userId = jo.getString("userId");
            Integer price = jo.getInt("price");
            Integer informationId = jo.getInt("informationId");
            if (userRepository.findById(Integer.valueOf(userId))==null) {
                result.put("errMsg", "用户不存在");
                result.put("returnValue", 10);
                return result;
            }
            Offer offer = new Offer();
            offer.setCreateTime(new Date(System.currentTimeMillis()));
            offer.setUserId(Integer.valueOf(userId));
            offer.setPrice(price);
            offer.setProduceId(informationId);
            offer.setType(0);
            offerRepository.save(offer);
            result.put("errMsg", "成功");
            result.put("returnValue", 1);
            return result;
        } catch (JSONException e){
            e.printStackTrace();
            result.put("errMsg","JSON参数异常");
            result.put("returnValue", 11);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errMsg","服务器异常");
            result.put("returnValue",3);
            return result;
        }
    }

    /**
     *
     * @api {GET} /api/offer/getOfferInfo 获取出价信息
     * @apiName getOfferInfo
     * @apiGroup offer
     * @apiVersion 0.0.1
     * @apiDescription 获取出价信息
     * @apiParam {String} informationId 资信编号
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功, 2:参数错误,3:服务器异常,8:无数据, 整型】,
     * "errMsg":【错误信息, 字符串】.
     * "data":[{
     *     "userId":【用户编号，字符串】
     *     "userName":【行家姓名,字符串】,
     *     "userHeadPicture":【行家头像，字符串】,
     *     "userIntroduce":【行家介绍，字符串】,
     *     "content":【资信内容,字符串】,
     *     "id":【编号，整型】
     *     "address":【资信地址,字符串】,
     *     "price":【资信价格,整型】,
     *     "images":[{
     *         "name":【图片，字符串】
     *     }],
     *     "type":【资信类型,0:文字，1:图片, 整型】
     * }]
     */
    @RequestMapping("getOfferInfo")
    public Map<String,Object> getOfferInfo(Integer informationId){
        Map<String,Object> result = new HashMap<>();
        if(informationId==null){
            result.put("errMsg","参数异常");
            result.put("returnValue",2);
            return result;
        }
        try {
            Information information=informationRepository.findById(informationId);
            if(information==null){
                result.put("errMsg","无数据");
                result.put("returnValue",8);
                return result;
            }
            User user = userRepository.findById(information.getId());
            List images = new ArrayList();
            //如果为图片
            if(information.getType()==1){
                List<InformationImage> informationImageRepositoryList = informationImageRepository.findByInfoId(informationId);
                for(InformationImage informationImage:informationImageRepositoryList){
                    Map imageMap = new HashMap();
                    imageMap.put("name",informationImage.getName());
                    images.add(imageMap);
                }
            }
            result.put("userId",information.getUserId().toString());
            result.put("content",information.getContent());
            result.put("id",information.getId());
            result.put("address",information.getAddress());
            result.put("price",information.getPrice());
            if(information.getType()==null){
                result.put("type",0);
            }else {
                result.put("type", information.getType());
            }
            result.put("userName",user.getName());
            result.put("userHeadPicture", WebApiConfig.IMAGE_URL+user.getHeadPicture());
            result.put("userIntroduce",user.getIntroduce());
            result.put("errMsg","成功");
            result.put("returnValue",1);
            result.put("images",images);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errMsg","服务器异常");
            result.put("returnValue",3);
            return result;
        }
    }
}
